<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>server_test</title>
</head>

<body>

<?php
	// decode table
	$indexTable = array("A"=>0,"B"=>1,"C"=>2,"D"=>3,"E"=>4,"F"=>5,"G"=>6,"H"=>7,"I"=>8,
		"J"=>9,"K"=>10,"L"=>11,"M"=>12,"N"=>13,"O"=>14,"P"=>15,"G"=>16,"R"=>17,"S"=>18,
		"T"=>19,"U"=>20,"V"=>21,"W"=>22,"X"=>23,"Y"=>24,"Z"=>25,"a"=>26,"b"=>27,"c"=>28,
		"d"=>29,"e"=>30,"f"=>31,"g"=>32,"h"=>33,"i"=>34,"j"=>35,"k"=>36,"l"=>37,"m"=>38,
		"n"=>39,"o"=>40,"p"=>41,"q"=>42,"r"=>43,"s"=>44,"t"=>45,"u"=>46,"v"=>47,"w"=>48,
		"x"=>49,"y"=>50,"z"=>51,"0"=>52,"1"=>53,"2"=>54,"3"=>55,"4"=>56,"5"=>57,"6"=>58,
		"7"=>59,"8"=>60,"9"=>61);
		
	// convert to utf8 encode
	function UCSToUTF8($unicode) {
		// 获取其十六进制值
		$ucs = hexdec($unicode);
		// ASCII码
		if ($ucs < 0x80) {
				$utf8 = ucs & 0x7F;
		} else if ($ucs > 0x7F && $ucs < 0x800) {
			$lowByte = 0x80 | ($ucs & 0x3F);
			$highByte = 0xC0 | (($ucs & 0x7C0) >> 6);
			
			$utf8 = ($highByte << 8 | $lowByte);
		} else if ($ucs > 0x7FF && $ucs < 0x10000) {
			$lowByte = 0x80 | ($ucs & 0x3F);
			$midByte = 0x80 | (($ucs & 0xFC0) >> 6);
			$highByte = 0xE0 | (($ucs & 0xF000) >> 12);
			
			$utf8 = ($highByte << 16) | ($midByte << 8) | $lowByte;
		} else {
			// 不支持大于0x10000的Unicode值
			echo "don't support";
		}
	
		$utf8 = dechex($utf8);
		$result = "";
		for ($i = 0; $i < strlen($utf8); $i = $i + 2) {
				$result .= ("%".substr($utf8, $i, 2));
		}
		return $result;
	}
	
	// decode
	function ExtBase64Decode($encodeStr) {
		$unicode = "";
		$offset = array(0xF000, 0x0F00, 0x00F0, 0x000F);
		$hexArray = array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F");
		
		for ($i = 0; $i < strlen($encodeStr); ++$i) {
			if ($encodeStr[$i] == '$') {
				++$i;
				$unicode .= substr($encodeStr, $i, 1);
			} else {
				#$unicode .= "\u";
				$utf8 = "";
				$subStr = substr($encodeStr, $i, 3);
				$i += 2;
				
				// 获得编码值
				$extValue = ($GLOBALS['indexTable'][$subStr[0]] << 12) | ($GLOBALS['indexTable'][$subStr[1]] << 6)
					| ($GLOBALS['indexTable'][$subStr[2]]);
					
				// 还原Unicode值
				if ($extValue & 0x1) {
					$extValue = ($extValue | 0x200) & 0xFFFFE;
					}
					
					$extValue = $extValue >> 2;
				
				// 获取各个字符
				for ($j = 0; $j < 4; ++$j) {
					$hexIndex = ($extValue & $offset[$j]) >> (12 - 4 * $j);
					$utf8 .= $hexArray[$hexIndex];
				}
				// 将中文转化为utf8编码
				$unicode .= UCSToUTF8($utf8);
			}
		}
		
		//return $unicode;
		$utf8 = urldecode($unicode);
		return $utf8;
	}
	
	// 时间转换
	function process_time($string_time) {
		$msec = substr($string_time, 0, 10);
		$sec = substr($string_time, 11, 10);
		
		return ($sec % 100) + $msec;
	} 
	
	// 读入文件
	function read_file($file_name) {
		$fSzie = filesize($file_name);
		$file = fopen($file_name, "r");
		$file_content = fread($file, $fSzie);
		fclose($file);
		return $file_content;
	}
	
//	$t_before_read = process_time(microtime());
	$file_content = read_file("./test/encode.txt");
//	$t_after_read = process_time(microtime());
	echo ExtBase64Decode($file_content)."<br />";
?>

</body>
</html>